package com.claa.springboot.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.claa.springboot.components.LoginInterceptor;
import com.claa.springboot.components.MyLocaleResolver;

//@EnableWebMvc
@Configuration
public class MyWebConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// TODO Auto-generated method stub
//		super.addViewControllers(registry);
//		registry.addViewController("/test").setViewName("success");
//		registry.addViewController("/").setViewName("login");
//		registry.addViewController("/index.html").setViewName("login");
	}
	
	
	/**
	 * 可以内置一个返回 WebMvcConfigurerAdapter 子类的实体类，配置也会生效
	 * @return WebMvcConfigurerAdapter
	 */
	@Bean
	public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
		WebMvcConfigurerAdapter webMvcConfigurerAdapter = new WebMvcConfigurerAdapter() {
			@Override
			public void addViewControllers(ViewControllerRegistry registry) {
				// TODO Auto-generated method stub
				// super.addViewControllers(registry);
				registry.addViewController("/").setViewName("login");
				registry.addViewController("/index.html").setViewName("login");
				registry.addViewController("/main.html").setViewName("dashboard");
			}
			
			@Override
			public void addInterceptors(InterceptorRegistry registry) {
				// 添加登陆拦截器
				// 不需要过滤静态资源的请求(*.css ...)，SpringBoot 已经配置好
				registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**")	// 过滤所有的请求
					.excludePathPatterns("/", "/index.html", "/user/login");					
			}
		};
		return webMvcConfigurerAdapter;
	}
	
	/**
	 * 自定义区域语言处理器
	 * @return
	 */
	@Bean
	public LocaleResolver localeResolver() {
		return new MyLocaleResolver();
	}
	
}
